package com.yycwxt.modules.admin.Return;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.Return;
import com.yycwxt.modules.admin.store.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api
@RestController
@Validated
@RequestMapping("/Returns")
public class ReturnController {

    private ReturnService returnService;

    @Autowired
    public ReturnController(ReturnService returnService){this.returnService = returnService;}

//    @GetMapping()
//    @ApiOperation("查询全部店铺打款")
//    public List<Return> selectAll(){return returnService.list();}

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询店铺打款")
    public Return selectById(@PathVariable Integer id){
        return returnService.getById(id);
    }

    @GetMapping("/getbyzq")
    @ApiOperation("根据账期查询店铺打款")
    public List<Return> getByZq(String zq){
        Map<String, Object> map = new HashMap<>();
        map.put("zq",zq);
        return returnService.listByMap(map);
    }

    @PutMapping()
    @ApiOperation("根据id更改店铺打款")
    public boolean updateSale(@ApiParam(name = "dpdk对象",required = true,type = "dpdk") @RequestBody Return dpdk){
        return returnService.updateById(dpdk);
    }

    @PostMapping("/upload")
    @ApiOperation("Excel导入数据")
    public Result excelToDatabase(@ApiParam(value = "是否删除全部历史记录", type = "boolean") @RequestParam boolean deleteHistory,
                                  @ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
        Result result = new Result();
        if(returnService.importFileToDB(deleteHistory, file)){
            result.setCode("200");
            result.setMsg("添加成功");
        }else {
            result.setCode("500");
            result.setMsg("添加失败");
        }
        return result;
    }

    @GetMapping("/data")
    @ApiOperation("获取数据")
    public ReturnResult getdata(int zq){
        return ReturnResult.buildSuccessResult(returnService.getreturn(zq));
    }

    // 查询下载文件地址
    @GetMapping("/getDpdkUrl")
    @ApiOperation("获取店铺打款文件的URL")
    public ReturnResult getUrl(){
        return ReturnResult.buildSuccessResult(returnService.getDpdkUrl());
    }
}
